import { Context } from 'koa'
import {
  request,
  summary,
  description,
  query,
  path,
  body,
  tags,
  prefix,
  security
} from 'koa-swagger-decorator'
import * as remarkSerivce from '../../service/remark'
import { generateToken } from '../../../core/auth'
import RedisClient from '../../../core/redis'
import CacheClient from '../../../core/cache'

const tag = tags(['remark'])

const remarkSchema = {
  id: { type: 'number', required: false },
  content: { type: 'string', required: true },
  courseId: { type: 'number', required: true },
  mouldId: { type: 'number', required: true }
}
const delSchema = {
  id: { type: 'number', required: true }
}
@prefix('/remark')
export default class RemarkController {
  @request('post', '/list')
  @summary('Get remarks')
  @description('example: /remark/list')
  @tag
  @security([{ api_key: [] }])
  async getList(ctx: Context) {
    global.UnifyResponse.success(ctx, await remarkSerivce.getAll())
  }

  @request('post', '/inserts')
  @summary('remark inserts')
  @description('example: /remark/inserts')
  @tag
  @body([])
  async inserts(ctx: Context) {
    const remarks = ctx.request.body
    global.UnifyResponse.success(ctx, await remarkSerivce.bulkBuildRemark(remarks))
  }

  @request('put', '/update')
  @summary('remark update')
  @description('example: /remark/update')
  @tag
  @body(remarkSchema)
  async update(ctx: Context) {
    const remark = ctx.request.body
    global.UnifyResponse.success(ctx, await remarkSerivce.updateRemark(remark.id, remark))
  }

  @request('post', '/byMould')
  @summary('Get remarss')
  @description('example: /remark/byMould')
  @tag
  @security([{ api_key: [] }])
  @body({
    id: { type: 'number', required: true }
  })
  async getListByCourse(ctx: Context) {
    const id = ctx.request.body.id
    global.UnifyResponse.success(ctx, await remarkSerivce.getRemarkByMould(id))
  }

  @request('delete', '/delete')
  @summary('remark delete')
  @description('example: /remark/delete')
  @tag
  @body(delSchema)
  async delete(ctx: Context) {
    const id = ctx.request.body.id
    global.UnifyResponse.success(ctx, await remarkSerivce.deleteById(id))
  }

}
